﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class MaterialCategoryManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindData();
            }
        }
        private void BindData()
        {
           // UART.BLL.Sales.MaterialCategory bll = new UART.BLL.Sales.MaterialCategory();
           var ds = mk.l.Sales_MaterialCategory.ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }
        /// <summary>
        /// Handles the RowEditing event of the myGridView control.设置编辑状态
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.myGridView.EditIndex = e.NewEditIndex;
            this.BindData();
        }

        /// <summary>
        /// Handles the RowCancelingEdit event of the myGridView control.取消编辑状态
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.myGridView.EditIndex = -1;
            this.BindData();
        }

        /// <summary>
        /// Handles the RowDeleting event of the myGridView control.删除数据
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewDeleteEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this.lblMsg.Text = string.Empty;
         //   UART.BLL.Sales.MaterialCategory bll = new UART.BLL.Sales.MaterialCategory();
    Sales_MaterialCategory    bll=    mk.l.Sales_MaterialCategory.Single(y => y.MaterialCategoryID == int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["MaterialCategoryID"].ToString()));
            try
            {
              //  bll.Delete();
                mk.l.Sales_MaterialCategory.DeleteOnSubmit(bll);
                mk.l.SubmitChanges();
            }
            catch
            {
                this.lblMsg.Text = "删除出错！";
            }
            //重新绑定数据
            this.BindData();
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.添加产品大类
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            this.lblMsg.Visible = false;
            if (this.IsValid)
            {
             //   UART.BLL.Sales.MaterialCategory bll = new UART.BLL.Sales.MaterialCategory();
                Sales_MaterialCategory m = new Sales_MaterialCategory();
                m.Name = mk.InputText(this.txtName.Text, 50);
                m.Notes = mk.InputText(this.txtNotes.Text, 100);
                try
                {
                   // bll.Add(m);
                    mk.l.Sales_MaterialCategory.InsertOnSubmit(m);
                    mk.l.SubmitChanges();
                    this.txtName.Text = "";
                    this.txtNotes.Text = "";
                    this.lblMsg.Visible = true;
                    this.lblMsg.Text = "添加成功！";
                    //UART.Common.MsgBox.Alert("添加成功！");
                    //重新绑定数据
                    this.BindData();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Visible = true;
                    this.lblMsg.Text = "添加失败！错误信息：" + ex.Message;
                    //UART.Common.MsgBox.Alert("添加失败！错误信息：" + ex.Message);
                }
            }
        }
        /// <summary>
        /// Handles the RowDataBound event of the myGridView control.绑定数据时如果是编辑行则显示编辑输入框
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Edit || ((int)e.Row.RowState) == 5)
                {
                    TextBox txtName = (TextBox)(e.Row.FindControl("txtName"));
                    if (txtName != null)
                    {
                        txtName.Text = this.myGridView.DataKeys[e.Row.RowIndex].Values["Name"].ToString();
                    }

                }

            }
        }

        /// <summary>
        /// Handles the RowUpdating event of the myGridView control.更新数据
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (this.IsValid)
            {
var bll =mk.l.Sales_MaterialCategory.ToList();
             Sales_MaterialCategory m = bll.Where(h=>h.MaterialCategoryID==int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["MaterialCategoryID"].ToString())).FirstOrDefault();
                if (m != null)
                {
                    TextBox txtName = (TextBox)(this.myGridView.Rows[e.RowIndex].FindControl("txtName"));
                    TextBox txtNotes = (TextBox)(this.myGridView.Rows[e.RowIndex].FindControl("txtNotes"));
                    m.Name = mk.InputText(txtName.Text, 50);
                    m.Notes = mk.InputText(txtNotes.Text, 100);
                  //  bll.Update(m);
                    mk.l.SubmitChanges();
                    this.myGridView.EditIndex = -1;
                    //重新绑定数据
                    this.BindData();
                }
            }
        }

        /// <summary>
        /// Handles the ServerValidate event of the CustomValidator1 control.验证是否存在重复产品大类
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
           var bll = mk.l.Sales_MaterialCategory.ToList();
            if (bll.Exists(y=>y.Name==mk.InputText(this.txtName.Text, 50)))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
        /// <summary>
        /// Handles the ServerValidate1 event of the CustomValidator1 control.验证是否存在重复产品大类
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
        {
            var bll = mk.l.Sales_MaterialCategory.ToList();
            CustomValidator val = (CustomValidator)source;
            //这里要引用父级控件，val的父级是单元格，再父级才是数据行
            GridViewRow gvr = (GridViewRow)(val.Parent.Parent);
          Sales_MaterialCategory m = bll.Where(h=>h.MaterialCategoryID==int.Parse(gvr.Cells[0].Text)).FirstOrDefault();
            TextBox txtName = (TextBox)(val.Parent.FindControl("txtName"));
            if (txtName != null)
            {
                string strName = mk.InputText(txtName.Text, 50);
                //要先判断一下，如果更改了产品大类的话，判断是否存在重复数据
                if (m.Name != strName)
                {
                    if (bll.Exists(y=>y.Name==strName))
                    {
                        args.IsValid = false;
                    }
                    else
                    {
                        args.IsValid = true;
                    }
                }
                else
                {
                    args.IsValid = true;
                }
            }
            else
            {
                args.IsValid = false;
            }
        }


    }
}
